package com.chy.blog_server.modular.system.controller;

import cn.hutool.core.util.StrUtil;
import com.chy.blog_server.modular.system.entity.SysMenu;
import com.chy.blog_server.modular.system.entity.vo.SysMenuTreeVO;
import com.chy.blog_server.modular.system.entity.vo.SysMenuTreeVOTwo;
import com.chy.blog_server.modular.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：菜单表 controller
 * 作者：chenhuiyan
 * 时间：2019/9/28 12:03
 */
@Controller
@RequestMapping(value = "/sys/menu")
public class SysMenuController {

    /**
     * 前缀
     */
    private static String PREFIX = "system/sysMenu/";

    @Autowired
    private ISysMenuService sysMenuService;

    /**
     * 查询菜单树列表
     */
    @RequestMapping(value = "/getMenuTreeList")
    @ResponseBody
    public Map<String, Object> getMenuTreeList() {
        Map<String, Object> res = new HashMap<>();
        try{
            SysMenuTreeVO sysMenuTreeVO = sysMenuService.getMenuTreeList();
            res.put("state", "200");
            res.put("message", "获取菜单树列表成功");
            res.put("data", sysMenuTreeVO);
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "获取菜单树列表失败");
        }

        return res;
    }

    /**
     * 跳转菜单管理页面
     * @return
     */
    @RequestMapping(value = "/index")
    public String index(){
        return PREFIX + "sysMenu";
    }

    /**
     * 获取菜单列表(菜单管理)
     * @return
     */
    @RequestMapping(value = "/getMenuListToManager")
    @ResponseBody
    public Map<String, Object> getMenuListToManager(){
        List<Map<String, Object>> data = sysMenuService.getMenuListToManager();
        Map<String, Object> res = new HashMap<>();
        res.put("code", "0");
        res.put("msg", "获取成功");
        res.put("count", data.size());
        res.put("data", data);
        return res;
    }

    /**
     * 根据id查找信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/getInfoById")
    @ResponseBody
    public Map<String, Object> getInfoById(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            res = sysMenuService.getInfoById(id);
            res.put("state", "200");
            res.put("message", "获取成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "获取失败");
        }
        return res;
    }

    /**
     * 跳转添加/编辑页面
     * @return
     */
    @RequestMapping(value = "/addOrEdit")
    public String addOrEdit(String id, Model model){
        model.addAttribute("id", id);
        return PREFIX + "sysMenu_add";
    }

    /**
     * 保存
     * @param sysMenu
     * @param request
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String, Object> saveData(SysMenu sysMenu, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try {
            sysMenu.setIcon("fa " + sysMenu.getIcon());

            SysMenu menu = sysMenuService.getById(sysMenu.getParentId()); //根据上级id查找上级菜单信息
            //如果上级菜单为最顶级，则不拼接
            if("0".equals(menu.getParentIds())){
                sysMenu.setParentIds(sysMenu.getParentId());
            }else{
                sysMenu.setParentIds(menu.getParentIds() + "," + sysMenu.getParentId());
            }

            if(StrUtil.isEmpty(sysMenu.getId())){
                sysMenu.preInsert(request);
                sysMenuService.save(sysMenu);
                res.put("state", "200");
                res.put("message", "添加成功");
            }else{
                sysMenu.preUpdate(request);
                sysMenuService.updateById(sysMenu);
                res.put("state", "200");
                res.put("message", "修改成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "添加失败");
        }
        return res;
    }

    /**
     * 查询菜单树列表（添加菜单树列表）
     */
    @RequestMapping(value = "/getMenuTreeListTwo")
    @ResponseBody
    public Map<String, Object> getMenuTreeListTwo() {
        Map<String, Object> res = new HashMap<>();
        try{
            SysMenuTreeVOTwo sysMenuTreeVOTwo = sysMenuService.getMenuTreeListTwo();
            res.put("state", "200");
            res.put("message", "获取菜单树列表成功");
            res.put("data", sysMenuTreeVOTwo);
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "获取菜单树列表失败");
        }

        return res;
    }

    /**
     * 删除
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(String id, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            SysMenu sysMenu = sysMenuService.getById(id);
            sysMenu.preUpdate(request);
            sysMenu.setDelFlag(1);
            sysMenuService.updateById(sysMenu);
            res.put("state", "200");
            res.put("message", "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除失败");
        }
        return res;
    }

}
